package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.adminDto.AdSensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.SensitiveDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Date;

/**
 * @author 石头
 * @description 针对表【wm_sensitive(敏感词信息表)】的数据库操作Service实现
 * @createDate 2022-06-24 17:09:15
 */
@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive>
        implements WmSensitiveService {

    /**
     * 敏感词查询列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findSensitiveList(SensitiveDto dto) {
        log.info("敏感词查询列表已经执行");
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery().orderByDesc(WmSensitive::getCreatedTime);
        // 分页查询
        IPage<WmSensitive> wmSensitivePage = new Page<>(dto.getPage(), dto.getSize());
        if (!StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(WmSensitive::getSensitives, dto.getName());
        }
        IPage<WmSensitive> page = page(wmSensitivePage, wrapper);
        ResponseResult responseResult = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 敏感词删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delSensitiveById(Integer id) {
        log.info("敏感词删除已经执行");
        if (id == null || id < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        boolean removeFlag = this.removeById(id);
        return removeFlag ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }

    /**
     * 敏感词新增
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitiveDto dto) {
        log.info("敏感词新增已经执行");
        if (dto == null || StringUtils.isBlank(dto.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String name = dto.getSensitives();

        // 敏感词已经存在直接返回
        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, name);
        WmSensitive wmSensitive = this.getOne(wrapper);

        if (wmSensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        // 敏感词新增
        WmSensitive wmSensitive1 = new WmSensitive();
        wmSensitive1.setCreatedTime(new Date());
        wmSensitive1.setSensitives(name);
        boolean saveFlag = this.save(wmSensitive1);

        return saveFlag ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }

    /**
     * 敏感词修改
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitiveDto dto) {
        log.info("敏感词修改已经执行");
        if (dto == null || dto.getId() < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer id = dto.getId();
        // 查询敏感词
        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getId, id);
        WmSensitive sensitive = this.getOne(wrapper);

        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 属性拷贝
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto, wmSensitive);

        // 修改敏感词
        boolean updateFlag = this.updateById(wmSensitive);

        return updateFlag ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }
}




